package com.donghu.study;

import org.apache.commons.lang3.RandomStringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.IOException;

@WebServlet(urlPatterns = "/goods/insert")
@MultipartConfig(location = "D：upload-images/",
                maxFileSize = 100*1024*1024,
                maxRequestSize = 100*1024*1024)
public class GoodsinserServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resq) throws IOException, ServletException {
        //接受参数
        String name = req.getParameter("n");
        String price = req.getParameter("p");
        Part file = req.getPart("i");

        String imgName= RandomStringUtils.randomAlphanumeric(10) + System.nanoTime();
//        String name = file.getSubmittedFileName();
        //保存文件
        file.write(imgName);
        //修改数据库
        String sql="INSERT INTO goods (`name`,price,image) VALUES(?,?,?)";
        JdbcUtils.modify(sql,name,price,imgName);

        resq.sendRedirect("/XyY/goods-list.jsp");
    }
}
